﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.Windows.Forms;
using NetJsWire.JsonRpcHandler;
using NetJsWire.JsonRpcHandler.Attributes;
using NetJsWire.Widgets.Core;
using NetJsWire.Widgets.Renderers.Core;

namespace NetJsWire.Widgets.Renderers.Form
{
    [ ClassName( "Ext.form.Radio" ) ]
    internal class Radio : Widget, IRenderer
    {
        public Radio( object baseObject )
            : base( baseObject )
        {
        }

        #region IRenderer Members

        public override void Render()
        {
            if ( !Rendered )
            {
                if ( BaseParent is IWidgetContainer )
                {
                    SetAttribute( ControlNode, "isInContainer", true );
                    SetConstructionProperty( "isInContainer", true );
                    SetConstructionProperty( "sendUpdates", false );

                    if ( BaseParent is Ui.Form.FormPanel || BaseParent is Ui.Form.FieldSet )
                    {
                        SetConstructionProperty( "fieldLabel", ( ( Ui.Form.Radio ) Base ).FieldLabel );

                        // The InputValue is automatically set to the InternalID to set the value for it when form is submitted
                        SetConstructionProperty( "inputValue", InternalID );
                    }
                }
                else
                {
                    SetStyleProperty( "position", "absolute" );
                    SetConstructionProperty( "parent", ClientParentToRender );
                }

                EnsureParentNodePosition();

                // We set the name of the Radio here to the name of the container to ensure every radio is on its corresponding group
                SetConstructionProperty( "name", "radio_" + ( BaseParent as IWidget ).Renderer.InternalID );

                SetConstructionProperty( "tabIndex", ( ( Ui.Form.Radio ) Base ).TabIndex );

                if ( !( ( Ui.Form.Radio ) Base ).AutoSize )
                    SetConstructionProperty( "width", ( ( Control ) Base ).ClientSize.Width );
                if ( ( ( Ui.Form.Radio ) Base ).Checked )
                    SetConstructionProperty( "checked", ( ( Ui.Form.Radio ) Base ).Checked );
            }
            base.Render();
        }

        #endregion
    }
}